<template>
  <el-dialog
    append-to-body
    :title="info"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    align="left"
    v-loading="currLoading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    width="600px">
    <el-form ref="form" v-if="dialogVisible" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="名称" align="left" prop="name">
        <el-input v-model="form.name" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="图片" align="left" prop="image">
        <el-upload
          class="avatar-uploader"
          :action="UploadFileUrl"
          :headers="getHeader()"
          name="myUploadFile"
          :show-file-list="false"
          :on-success="uploadImageSuccess">
          <img v-if="form.image" :src="form.image" class="avatar" style="width: 100px">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-input readonly v-model="form.image" style="width: 200px"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'CreateSupplierBrandDialog',
  components: {},
  props: {
    successFunction: Function
  },
  data: function () {
    return {
      info: '创建',
      dialogVisible: false,
      currLoading: false,
      form: {
        id: 0,
        name: '',
        image: '',
      },
      rules: {
        name: [
          {required: true, message: '请输入 名称', trigger: ['blur', 'change']},
          {min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: ['blur', 'change']}
        ],
        image: [
          {required: true, message: '请上传 图片', trigger: ['blur', 'change']},
        ]
      }
    }
  },
  mounted () {
    this.getRoleArr()
  },
  methods: {
    getRoleArr:function () {
      let $this=this
      $this.MyRequest($this.ApiDoNameSupplier + 'User/getRoleMap', {userType:'Supplier'}, function (data) {
        $this.currLoading = false
        $this.dialogVisible = false
        $this.roleArr=$this.MapToArray(data)
        $this.$forceUpdate()
      }, function () {
        $this.$message.success('操作失败请重试')
        $this.currLoading = false
        $this.$forceUpdate()
      })
    },
    uploadImageSuccess: function (res) {
      this.form.image = res.data
    },
    open: function (infoData) {
      let $this = this
      $this.info = '创建品牌'
      if (infoData.id) {
        $this.info = '更新品牌'
        $this.form.id = infoData.id
        $this.form.name = infoData.name
        $this.form.image = infoData.image
      } else {
        $this.form = {
          id: 0,
          name: '',
          image: '',
        }
        if ($this.$refs.form) {
          $this.$refs.form.resetFields()
        }
      }
      $this.dialogVisible = true
      $this.$forceUpdate()
    },
    submit: function () {
      let $this = this
      $this.$refs.form.validate((valid) => {
        if (valid) {
          var api = 'SupplierBrand/create'
          if (this.form.id > 0) {
            api = 'SupplierBrand/update'
          }
          $this.currLoading = true
          $this.MyRequest(this.ApiDoNameSupplier + api, $this.form, function () {
            $this.$message.success($this.info + '成功')
            $this.currLoading = false
            $this.form = {
              id: 0,
              name: '',
              image: '',
            }
            $this.dialogVisible = false
            if (typeof $this.$props.successFunction === 'function') {
              $this.$props.successFunction()
            }
            $this.$forceUpdate()
          }, function () {
            $this.$message.success('操作失败，请重试')
            $this.currLoading = false
            $this.$forceUpdate()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
